#include"Common.h"
#include"ConcurrentAlloc.h"

void Routine()
{
	int* tmp = (int*)ConcurrentAlloc(4);
	//ConcurrentFree(tmp, 4);
}
void TestThread()
{
	std::thread t1(Routine);
	std::thread t2(Routine);

	t1.join();
	t2.join();
}
void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);

	cout << p1 << endl;		
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
}
void TestConcurrentAlloc2()
{
	for (size_t i = 0; i < 1024; ++i)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << p1 << endl;
	}

	void* p2 = ConcurrentAlloc(8);
	cout << p2 << endl;
}
void TestFree1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);
	void* p6 = ConcurrentAlloc(8);
	void* p7 = ConcurrentAlloc(8);

	*(int*)p1 = 1;
	*(int*)p2 = 1;
	*(int*)p3 = 1;
	*(int*)p4 = 1;
	*(int*)p5 = 1;
	*(int*)p6 = 1;
	*(int*)p7 = 1;

	//ConcurrentFree(p1,6);
	//ConcurrentFree(p2, 8);
	//ConcurrentFree(p3, 1);
	//ConcurrentFree(p4, 7);
	//ConcurrentFree(p5, 8);
	//ConcurrentFree(p6, 8);
	//ConcurrentFree(p7, 8);
}

void ThreadRandler()
{
	std::vector<void*> t;
	for (int i = 0; i < 7; i++)
	{
		void* tmp = ConcurrentAlloc(8);
		t.push_back(tmp);
	}
	
	for (int i = 0; i < 7; i++)
	{
		//ConcurrentFree(t[i], 8);
	}
}
void TestThreadFree()
{
	std::thread t1(ThreadRandler);
	std::thread t2(ThreadRandler);

	t1.join();
	t2.join();
}

void TestBigAlloc()
{
	void* ptr1 = ConcurrentAlloc(257 * 1024);
	//ConcurrentFree(ptr1, 257 * 1024);

	void* ptr2 = ConcurrentAlloc(256 * 1024 * 8);
	//ConcurrentFree(ptr2, 256 * 8 * 1024);
}

void TestBigFree()
{
	void* ptr1 = ConcurrentAlloc(257 * 1024);

	void* ptr2 = ConcurrentAlloc(129 * 8 * 1024);

	void* ptr3 = ConcurrentAlloc(6);

	ConcurrentFree(ptr1);
	ConcurrentFree(ptr2);
	ConcurrentFree(ptr3);

}
//int main()
//{
//	TestBigFree();
//	//TestBigAlloc();
//
//	//TestThreadFree();
//
//	//TestFree1();
//
//	//TestConcurrentAlloc2();
//	//TestConcurrentAlloc1();
//	//TestThread();
//	return 0;
//}